package cn.stylefeng.guns.modular.system.service.Myservice;

import cn.stylefeng.guns.modular.system.entity.BothSale;
import cn.stylefeng.guns.modular.system.entity.BothSaleCommit;
import cn.stylefeng.guns.modular.system.entity.BothSaleCommitCheck;
import cn.stylefeng.guns.modular.system.mapper.BothSaleCommitMapper;
import cn.stylefeng.guns.modular.system.model.params.paramsDTO.BothAreaDTO;
import cn.stylefeng.guns.modular.system.model.params.paramsDTO.CheckDTO;
import cn.stylefeng.guns.modular.system.service.*;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.core.util.utility.StatusCode;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author : xiaofei
 * @Date: 2019/7/11
 */
@Service
public class BothSaleOrderService extends ServiceImpl<BothSaleCommitMapper, BothSaleCommit> {


    @Autowired
    private BothSaleCommitService bothSaleCommitService;
    @Autowired
    private BothSaleCommitCheckService bothSaleCommitCheckService;
    @Autowired
    private BothAreaProductService bothAreaProductService;
    @Autowired
    private CheckBothSaleCommitService checkBothSaleCommitService;

    @Resource
    private RuntimeService runtimeService;
    @Resource
    private IdentityService identityService;
    @Resource
    private TaskService taskService;
    @Autowired
    private BothSaleService bothSaleService;

    /**
     * 提交
     * @param bothAreaDTO
     * @return
     */
    public ResponseData commitCheck(BothAreaDTO bothAreaDTO){

        //出入经销商提交表中
        BothSaleCommit bothSaleCommit = new BothSaleCommit();
        bothSaleCommit.setUserId(ShiroKit.getUserNotNull().getId());

        //1.设置用户选择经纬度
        String[] ltl = bothAreaDTO.getCustomerLongitudeLatitude().split(",");
        bothSaleCommit.setLongitude(ltl[0]);
        bothSaleCommit.setLatitude(ltl[1]);
        bothSaleCommit.setUserId(ShiroKit.getUserNotNull().getId());
        bothSaleCommit.setTime(new Date());
        bothSaleCommit.setCheckCode(StatusCode.PENDING.getVal());
        bothSaleCommit.setAddress(bothAreaDTO.getAddress());
//        bothSaleCommitService.save(bothSaleCommit);
//
//        //插入审核表中
//        BothSaleCommitCheck bothSaleCommitCheck = new BothSaleCommitCheck();
//
//        //找到刚插入表格的id
//        BothSaleCommit bothSaleCommitLast = bothSaleCommitService.getOne(new QueryWrapper<BothSaleCommit>()
//        .lambda().orderByDesc(BothSaleCommit::getId).last("limit 1"));
//        bothSaleCommitCheck.setBothSaleCommitId(bothSaleCommitLast.getId());
//        bothSaleCommitCheck.setCheckCode(StatusCode.PENDING.getVal());
//        bothSaleCommitCheck.setTime(new Date());
//        bothSaleCommitCheckService.save(bothSaleCommitCheck);
        identityService.setAuthenticatedUserId(ShiroKit.getUserNotNull().getId() + "");
        //启动流程实例，字符串"vacation"是BPMN模型文件里process元素的id
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("both_sale_commit");
        bothSaleCommit.setProcessInstanceId(processInstance.getId());
        bothSaleCommitService.save(bothSaleCommit);
        //流程实例启动后，流程会跳转到经销商提交
        Task vacationApply = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        //设置经销商提交任务的执行人
        taskService.setAssignee(vacationApply.getId(), ShiroKit.getUserNotNull().getId() + "");
        //完成经销商提交任务
        taskService.complete(vacationApply.getId());

        return  ResponseData.success();
    }


    /**
     * 审核
     * @param checkDTO
     * @return
     */
    public  ResponseData check(CheckDTO checkDTO){

        if (checkDTO.getCode() == StatusCode.SUCCESS.getVal()){
            //1如果审核成功会分配一个经销商
            BothSaleCommitCheck bothSaleCommitCheck = bothSaleCommitCheckService.getById(checkDTO.getId());
            BothSaleCommit bothSaleCommit = bothSaleCommitService.getById(bothSaleCommitCheck.getBothSaleCommitId());

            bothSaleCommit.setDealerDeptId(checkDTO.getDealerDeptId());
            bothSaleCommitService.getBaseMapper().updateById(bothSaleCommit);


            //更改审核表


        }
        return  null;
    }


    public void commitBothSale(BothSale bothsale) {
        //出入经销商提交表中
        bothsale.setUserId(ShiroKit.getUserNotNull().getId());
        bothsale.setCreateTime(new Date());

//        bothSaleCommitService.save(bothSaleCommit);
//
//        //插入审核表中
//        BothSaleCommitCheck bothSaleCommitCheck = new BothSaleCommitCheck();
//
//        //找到刚插入表格的id
//        BothSaleCommit bothSaleCommitLast = bothSaleCommitService.getOne(new QueryWrapper<BothSaleCommit>()
//        .lambda().orderByDesc(BothSaleCommit::getId).last("limit 1"));
//        bothSaleCommitCheck.setBothSaleCommitId(bothSaleCommitLast.getId());
//        bothSaleCommitCheck.setCheckCode(StatusCode.PENDING.getVal());
//        bothSaleCommitCheck.setTime(new Date());
//        bothSaleCommitCheckService.save(bothSaleCommitCheck);
        identityService.setAuthenticatedUserId(ShiroKit.getUserNotNull().getId() + "");
        //启动流程实例，字符串"vacation"是BPMN模型文件里process元素的id
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("both_sale_commit");
        bothsale.setProcessInstanceId(processInstance.getId());
        bothSaleService.save(bothsale);
        //流程实例启动后，流程会跳转到经销商提交
        Task vacationApply = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        //设置经销商提交任务的执行人
        taskService.setAssignee(vacationApply.getId(), ShiroKit.getUserNotNull().getId() + "");
        //完成经销商提交任务
        taskService.complete(vacationApply.getId());
    }
}
